<template>
  <div>
    <el-card shadow="always" :body-style="{ padding: '20px' }">
      <el-form :inline="true" class="demo-form-inline" :disabled="disabled">
        <el-form-item label="一级分类">
          <el-select
            placeholder="请选择一级分类"
            v-model="CG1Id"
            @change="CG1IdChange"
          >
            <el-option
              v-for="item in CG1List"
              :label="item.name"
              :value="item.id"
              :key="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="二级分类">
          <el-select
            placeholder="请选择二级分类"
            v-model="CG2Id"
            @change="CG2IdChange"
          >
            <el-option
              v-for="item in CG2List"
              :label="item.name"
              :value="item.id"
              :key="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="三级分类">
          <el-select placeholder="请选择三级分类" v-model="CG3Id">
            <el-option
              v-for="item in CG3List"
              :label="item.name"
              :value="item.id"
              :key="item.id"
            />
          </el-select>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script lang="ts">
import { defineComponent } from "vue";
import { ElMessage } from "element-plus";

export default defineComponent({
  name: "Category",
});
</script>

<script setup lang="ts">
import useCateGoryStore from "@/stores/category";
import { onMounted } from "vue";
import { storeToRefs } from "pinia";
const CGStore = useCateGoryStore();

defineProps<{
  disabled: boolean;
}>();
// 获取一级分类的数据
const { CG1List, CG1Id, CG2List, CG2Id, CG3List, CG3Id } = storeToRefs(CGStore);

const CG1IdChange = () => {
  CG2List.value = [];
  CG2Id.value = null;
  CG3List.value = [];
  CG3Id.value = null;
  //发送2List的请求
  CGStore.getCG2List();
};
const CG2IdChange = () => {
  //每次当2重选的时候,要把3给清理掉
  CG3List.value = [];
  CG3Id.value = null;
  //发送2List的请求
  CGStore.getCG3List();
};
onMounted(() => {
  try {
    CGStore.getCG1List();
  } catch (e: any) {
    ElMessage.error(e.message);
  }
});
</script>
